#!/bin/bash


create_switch(){
	if [[ -z $1 ]]; then 
		echo 请传入一个交换机的名称，该脚本将返回用于qemu 网卡的启动脚本所在文件地址
		return 1
	fi

	switch_name=$1

	switch_script="$PWD/firmware_script/connect_switch$switch_name.sh"


	cat << EOF > $switch_script
#!/bin/bash


	if ! ip link show $switch_name >/dev/null 2>&1 ; then

		brctl addbr $switch_name
	fi
	
	brctl addif $switch_name \$1

	ip link set \$1 up

	ip link set $switch_name up


EOF
	chmod +x $switch_script

	echo $switch_script
}



add_ovs_tap_pair(){

	br_name=$1
	tap1_name=$2
	tap2_name=$3

	if ! systemctl is-active openvswitch-switch > /dev/null 2>&1; then

		sudo systemctl enable --now openvswitch-switch

		systemctl status openvswitch-switch

		journalctl -eu openvswitch-switch

	fi



	if ! sudo ovs-vsctl list-br | grep $br_name > /dev/null 2>&1; then
	
		sudo ovs-vsctl add-br $br_name

	fi


	if ! ip link show $tap1_name > /dev/null 2>&1; then


		sudo ip tuntap add $tap1_name mode tap

		sudo ovs-vsctl add-port $br_name $tap1_name

	fi

	if ! ip link show $tap2_name > /dev/null 2>&1; then


		sudo ip tuntap add $tap2_name mode tap

		sudo ovs-vsctl add-port $br_name $tap2_name

	fi


	sudo ip link set $tap1_name up
	sudo ip link set $tap2_name up
	sudo ip link set $br_name up
}

